package sk.tuke.magsa.tools.metamodel;

import sk.tuke.magsa.tools.metamodel.constraints.Constraint;
import yajco.annotation.Before;
import yajco.annotation.Exclude;
import yajco.annotation.Range;
import yajco.annotation.Separator;
import yajco.annotation.reference.Identifier;

public class Property implements Named {

    @Identifier(unique = "../sk.tuke.magsa.tools.metamodel.Property")
    private final String name;
    private final Type type;
    private Constraint[] constraints;
    
    @Exclude
    public Property(String name, Type type) {
        this.name = name;       
        this.type = type;
    }
    
    public Property (String name, @Before(":") Type type, @Separator(",") Constraint[] constraints)
    {
        this.name = name;
        this.type = type;
        this.constraints = constraints;
    }

    public String getName() {
        return name;
    }
    
    public Type getType()
    {
        return type;
    }

    @Override
    public String toString() {
        return name + " : " + type;
    }
    
    public Constraint[] getConstraints() {
        return constraints;
    }

    public void setConstraints(Constraint... constraints) {
        this.constraints = constraints;
    }
    
    public <T extends Constraint> T getConstraint(Class<T> clazz)
    {
        T con = null;
        for(Constraint c : constraints)
        {
            if(c.getClass().equals(clazz.getClass()))
            {
                con = (T)c;
                break;
            }
        }
        return con;
    }
    
    public boolean hasConstraint(Class<? extends Constraint> clazz)
    {
        Boolean constraintExists = false;
        for(Constraint c : constraints)
        {
            if(c.getClass().equals(clazz.getClass()))
            {
                constraintExists = true;
                break;
            }
        }
        return constraintExists;
    }
}
